package com.bocloud.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * rabbitmq配置类
 */
@Configuration
@Component
@EnableRabbit
public class RabbitmqConfig {

    Logger log = LoggerFactory.getLogger(RabbitmqConfig.class);


    @Bean
    public Queue queue() {
        log.info("建立队列成功");
        return new Queue("login");
    }


    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("directExchange");
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(directExchange()).with("login");
    }

    /*topic Exchange的测试，topic Exchange绑定两个队列，login和login1，routingKey分别为login.new 和 login.# */
    @Bean
    public Queue queue1() {
        return new Queue("login1");
    }

    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }

    @Bean
    public Binding binding1() {
        return BindingBuilder.bind(queue1()).to(topicExchange()).with("login.#");
    }

    @Bean
    public Binding binding2() {
        return BindingBuilder.bind(queue()).to(topicExchange()).with("login.new");
    }

    @Bean
    public Queue queue2() {
        return new Queue("login2");
    }

    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    public Binding binding3() {
        return BindingBuilder.bind(queue()).to(fanoutExchange());
    }

    @Bean
    public Binding binding4() {
        return BindingBuilder.bind(queue1()).to(fanoutExchange());
    }

    @Bean
    public Binding binding5() {
        return BindingBuilder.bind(queue2()).to(fanoutExchange());
    }

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }


    //queue listener 观察 监听模式 当有消息到达时会通知监听在对应的队列上的监听对象
    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        // factory.setPrefetchCount(5);//指定一个请求能处理多少个消息，如果有事务的话，必须大于等于transaction数量.
        factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
        //MANUAL：将ACK修改为手动确认，避免消息在处理过程中发生异常造成被误认为已经成功消费的假象。
        //factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        return factory;
    }

}
